package com.chaoshi.strategy.impl;

import com.chaoshi.strategy.PriceStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class DiscountStrawberryDecorator implements PriceStrategy {
    private final PriceStrategy decorated;  // 被装饰的策略对象
    private final double discountRate;  // 折扣率

    public DiscountStrawberryDecorator(PriceStrategy decorated, double discountRate) {
        this.decorated = decorated;
        this.discountRate = discountRate;
    }

    @Override
    public BigDecimal calculate(int a, int s, int m) {
        BigDecimal original = decorated.calculate(a, s, m);
        // 获取草莓单价并计算折扣
        if (decorated instanceof AllFruitsStrategy strategy) {
            BigDecimal strawberryPrice = strategy.getPrices().getStrawberry();
            BigDecimal discount = strawberryPrice
                    .multiply(new BigDecimal(s))
                    .multiply(BigDecimal.ONE.subtract(new BigDecimal(discountRate)))
                    .setScale(2, RoundingMode.HALF_UP);
            return original.subtract(discount);
        }
        return original;
    }
}
